package art.yulang.process;

import art.yulang.adapter.Adapter;
import art.yulang.server.Request;
import art.yulang.server.Response;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

public class Processor implements Runnable {

    private final Socket socket;
    private final Adapter adapter;

    public Processor(Socket socket, Adapter adapter){
        this.socket = socket;
        this.adapter = adapter;
        System.out.println(this.getClass().getName()+" recv a request " + socket.getLocalAddress().toString() +" port:"+socket.getPort());
    }

    @Override
    public void run() {
        try {
            InputStream inputStream = socket.getInputStream();
            OutputStream outputStream = socket.getOutputStream();

            Request request = new Request(inputStream);
            Response response = new Response(outputStream);

            adapter.service(request, response);

            socket.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
